package com.lse_api.config.security;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * spring security 工具类
 */
@Component
public class SecurityUtil {

    /**
     * 获取保存在spring security中的用户名
     *
     * @return
     */
    public static String getName() {
        return SecurityContextHolder.getContext().getAuthentication().getName();
    }


    /**
     * 获取用户权限列表
     *
     * @return
     */
    public static List<String> getAuth() {
        Collection<? extends GrantedAuthority> collection = SecurityContextHolder.getContext().getAuthentication().getAuthorities();

        List<String> list = collection.stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());

        return list;

    }

}
